// subpkg_worker/modifyelderMsg/modifyelderMsg.js
import Dialog from '@vant/weapp/dialog/dialog';
import Toast from '@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show:false,
      title:'',
      columns:[],
      elderId:'',
      elderlyName: '',
      sex: '',
      bedNumber: '',
      selfCareAbility: '',
      age: '',
      awareness: '',
      idCardNumber: '',
      admissionDate: '',
      dischargeDate: '',
      currentCareHomeId:'',
      createTime: '',
      image: '',
      sex1:''
  },
  click_selfCareAbility(){
    var columns = ['无需依赖','轻度依赖','中度依赖','重度依赖']
    this.setData({
      title:'自理能力',
      show:true,
      columns:columns
    })
  },

  click_awareness(){
    var columns = ['无认知障碍','轻度认知障碍','中度认知障碍','重度认知障碍']
    this.setData({
      title:'认知能力',
      show:true,
      columns:columns
    })
  },
  onClose(){
    this.setData({
      show:false
    })
  },
  onCancel(){
    this.setData({
      show:false
    })
  },
  onConfirm(e){
    if(this.data.title=='自理能力'){
      this.setData({
        selfCareAbility:e.detail.value
      })
    }
    else{
      this.setData({
        awareness:e.detail.value
      })
    }
    this.setData({
      show:false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */

   //id=1&bed=172&name=川悠&age=60&sex=男&selfCare_ability=不能自理&image=https://zhixiao.site:8080/client/elder/381b8cf7688146068a946818f46ab5ba.jpeg&cognitiveSituation=重度认知障碍
  onLoad(options) {
    this.setData({
      elderId:options.id,
    })
    this.queryElderMsg()
  },
  queryElderMsg(){
    wx.request({
      url: 'https://zhixiao.site:8080/client/elder/getMessage',
      data:{
        elderId:this.data.elderId
      },
      success:(res)=>{
        console.log(res)
        var msg = res.data.data
        this.setData({
          elderlyName: msg.elderlyName,
          sex: msg.sex,
          bedNumber: msg.bedNumber,
          selfCareAbility: msg.selfCareAbility,
          age: msg.age,
          awareness: msg.awareness,
          idCardNumber: msg.idCardNumber,
          admissionDate: msg.admissionDate,
          dischargeDate: msg.dischargeDate,
          currentCareHomeId:msg.currentCareHomeId,
          createTime: msg.createTime,
          image: msg.image,
        })
        if(this.data.sex==='男'){
          this.setData({
            sex:1,
            sex1:'男'
          })
        }else{
          this.setData({
            sex:0,
            sex1:'女'
          })
        }
      }
    })
  },
  confirm(){
    if(this.data.bedNumber===''||this.data.elderlyName===''||this.data.sex===''||this.data.age===''||this.data.selfCareAbility===''||this.data.awareness===''){
      Toast('请完善信息!')
      return
    }
    Dialog.confirm({
      title: '标题',
      message: '是否确定本次编辑',
    })
      .then(() => {
        wx.request({
          url: 'https://zhixiao.site:8080/client/elder/update',
          method:'PUT',
          header: {  
            "Content-Type": "application/json"
          },
          data:{
            elderlyId:this.data.elderId,
            bedNumber:this.data.bedNumber,
            elderlyName:this.data.elderlyName,
            age:this.data.age,
            sex:this.data.sex,
            selfCareAbility:this.data.selfCareAbility,
            awareness:this.data.awareness,
            idCardNumber:this.data.idCardNumber,
            admissionDate:this.data.admissionDate,
            dischargeDate:this.data.dischargeDate,
            createTime:this.data.createTime,
            currentCareHomeId:this.data.currentCareHomeId,
            image:this.data.image
          },
          success:(res)=>{
            console.log(res)
            Toast('编辑成功');

          }
        })
      })
      .catch(() => {
        // on cancel
        Toast('修改失败')
      });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})